package cn.cvs.controller;

import cn.cvs.pojo.SysUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

//http://localhost:8080/ch09_mvc/user/logout
@Controller
@RequestMapping("/user")
public class UserController {

    /**
     *
     * @param request
     * @param username 变量名 要和 前端 form表单中的 name属性一致
     * @param pwd
     * @return
     */
    @RequestMapping("/dologin")
    public String login(Model model
                        , HttpServletRequest request
                        , @RequestParam String username
                        , @RequestParam(name = "password",required = true,defaultValue = "1") String pwd){

        model.addAttribute("cly","曹良玉你好");

        SysUser tcx = new SysUser(11,"汤晨曦");

        SysUser lz = new SysUser(12,"刘珍");

        model.addAttribute("tcx",tcx);

        model.addAttribute(lz);  //map.put("sysUser",lz)

        model.addAttribute(tcx);  //map.put("sysUser",tcx)


        /*System.out.println("UserController>>login");
        System.out.println("username: "+ username);
        System.out.println("pwd: "+ pwd);
        if (username.equals("admin")&& pwd.equals("123456")){ //User user = service.login(username,pwd)
            request.getSession().setAttribute("username",username);
        }*/
        return "/user/userlist.jsp";
    }



    //(name = "password")  和前端的form表单的name属性相同
    //pwd321 是你起的别名
    @RequestMapping("/logout")
    public String logout(Map<String,Object> map  ){
        map.put("username321","tangchengxi");  //map.put(key)
        map.put("pwdAAA","6666666");
        System.out.println("UserController>>logout");
        return "/user/userlist.jsp";
    }
}
